angular.module('starter.services', [])

.service('EssayService', ['$http', '$q', function($http, $q){
	return {
		getEssayList: function(condition){
			var query = [];			
			if(condition.pagingMetaData && condition.pagingMetaData.pageIndex)
				query.push("pageIndex="+condition.pagingMetaData.pageIndex);
			if(condition.pagingMetaData && condition.pagingMetaData.pageSize)
				query.push("pageSize="+condition.pagingMetaData.pageSize);

			if(typeof condition.searchType != "undefined")
				query.push("searchType="+condition.searchType);

			if(condition.searchCriteria && condition.searchCriteria.id)
				query.push("id="+condition.searchCriteria.id);

			return $http.get("/essay/get-essay-list?" + query.join("&"));


			var deferred = $q.defer();
			deferred.resolve({
				data:{status: 1, result:[], total:0}
			});
      		return deferred.promise;
      		
		},
		getEssay: function(id){
			return $http.get("/essay/get-essay?id=" + id);      		
		}
	};
}])

.service('TagService', ['$http', '$q', function($http, $q){
	return {
		getTagList: function(condition){
			var query = [];			
			if(condition.pagingMetaData && condition.pagingMetaData.pageIndex)
				query.push("pageIndex="+condition.pagingMetaData.pageIndex);
			if(condition.pagingMetaData && condition.pagingMetaData.pageSize)
				query.push("pageSize="+condition.pagingMetaData.pageSize);

			if(typeof condition.searchType != "undefined")
				query.push("searchType="+condition.searchType);

			if(condition.searchCriteria && condition.searchCriteria.id)
				query.push("id="+condition.searchCriteria.id);

			return $http.get("/tag/get-tag-list?" + query.join("&"));


			condition.pagingMetaData.pageIndex == condition.pagingMetaData.pageIndex<=0?0:condition.pagingMetaData.pageIndex;
			var start = condition.pagingMetaData.pageIndex*condition.pagingMetaData.pageSize+1,
				end = (condition.pagingMetaData.pageIndex+1)*condition.pagingMetaData.pageSize+1

			var deferred = $q.defer();

			var itemList = [];
			for (var i = start; i < end; i++) {
				itemList.push({id:i, title:"t_"+i});
			}
			setTimeout(function(){
				deferred.resolve({
					data:{status: 1, result:itemList, total:32}
				});
			}, 4*1000);
      		return deferred.promise;
		}
	};
}])